package com.chuiyan.topnews;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;


public class Main extends AppCompatActivity implements View.OnClickListener {
    private FrameLayout frameLayout; //容器
    private Home home;
    private video video;
    private Hot hot;
//    private Task task;
    private User user;
    private ImageView bt_home;
    private ImageView bt_video;
    private ImageView bt_hot;
//    private ImageView bt_task;
    private ImageView bt_user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        frameLayout = findViewById(R.id.container);
        bt_home = findViewById(R.id.bt_home);
        bt_video = findViewById(R.id.bt_video);
        bt_hot = findViewById(R.id.bt_hot);
//        bt_task = findViewById(R.id.bt_task);
        bt_user = findViewById(R.id.bt_user);
        home = new Home();
        video = new video();
        hot = new Hot();
//        task = new Task();
        user = new User();
        show(frameLayout,home);//默认显示首页
        bt_home.setOnClickListener(this);
        bt_video.setOnClickListener(this);
        bt_hot.setOnClickListener(this);
//        bt_task.setOnClickListener(this);
        bt_user.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.bt_home:
                show(frameLayout,home);
                changeColor();
                bt_home.setImageResource(R.drawable.ic_home_click);
                break;
            case R.id.bt_video:
                show(frameLayout, video);
                changeColor();
                bt_video.setImageResource(R.drawable.ic_video_click);
                break;
            case R.id.bt_hot:
                show(frameLayout,hot);
                changeColor();
                bt_hot.setImageResource(R.drawable.ic_hot_click);
                break;
//            case R.id.bt_task:
//                show(frameLayout,task);
//                changeColor();
//                bt_task.setImageResource(R.drawable.ic_task_click);
//                break;
            case R.id.bt_user:
                show(frameLayout,user);
                changeColor();
                bt_user.setImageResource(R.drawable.ic_user_click);
                break;
            default:
                break;
        }
    }

    //切换菜单视图
    public void show(View v, Fragment fragment){
        //创建Fragment对象;
        //得到FragmentManager;
        FragmentManager manager = getSupportFragmentManager();
        //得到FragmentTransaction
        FragmentTransaction transaction = manager.beginTransaction();
        //将当前操作添加到回退栈
        transaction.addToBackStack(null);
        //替换Fragment对象并提交
        transaction.replace(v.getId(),fragment).commit();
    }

    //菜单改变颜色
    public void changeColor()
    {
        bt_home.setImageResource(R.drawable.ic_home);
        bt_video.setImageResource(R.drawable.ic_video);
        bt_hot.setImageResource(R.drawable.ic_hot);
//        bt_task.setImageResource(R.drawable.ic_task);
        bt_user.setImageResource(R.drawable.ic_user);
    }
}
